package com.yin.spring5.service;

import com.yin.spring5.dao.BookDao;
import com.yin.spring5.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * BookService
 *
 * @author Yin Guiqing
 */
@Service
public class BookService {
    BookDao bookDao;

    @Autowired
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public void addBook(Book book) {
        int i = bookDao.insert(book);
        System.out.println("受影响行数：" + i);
    }

    public void updateBook(Book book) {
        int i = bookDao.update(book);
        System.out.println("受影响行数：" + i);
    }

    public void deleteBook(Integer id) {
        int i = bookDao.delete(id);
        System.out.println("受影响行数：" + i);
    }

    public void recordCount() {
        int count = bookDao.recordCount();
        System.out.println("记录数：" + count);
    }

    public void getBookById(Integer id) {
        Book book = bookDao.getOneById(id);
        System.out.println("查询到：" + book);
    }

    public void getAllBooks() {
        List<Book> bookList = bookDao.getAll();
        System.out.println("全部记录（共 " + bookList.size() + " 条）：");
        bookList.forEach(System.out::println);
    }

    public void batchAddBook(List<Object[]> bookList) {
        int[] ints = bookDao.batchInsert(bookList);
        System.out.println(Arrays.toString(ints));
    }

    public void batchUpdateBook(List<Object[]> bookList) {
        int[] ints = bookDao.batchUpdate(bookList);
        System.out.println(Arrays.toString(ints));
    }

    public void batchDeleteBook(List<Object[]> idList) {
        int[] ints = bookDao.batchDelete(idList);
        System.out.println(Arrays.toString(ints));
    }
}
